package com.hao.study.controller;

import com.github.pagehelper.PageInfo;
import com.hao.study.common.APIResponse;
import com.hao.study.common.ResponeCode;
import com.hao.study.entity.Classification;
import com.hao.study.permission.Access;
import com.hao.study.permission.AccessLevel;
import com.hao.study.service.ClassificationService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: author
 * @description:  岗位分类
 * @date: 2024/6/11 17:55
 * @version: 1.0
 */
@Api(tags={"分类管理接口"})
@RestController
@RequestMapping("/classification")
public class ClassificationController {
    @Autowired
    ClassificationService classificationService;

    @GetMapping("/list")
    public APIResponse list(@RequestParam(defaultValue = "1") Integer pageNum,
                            @RequestParam(defaultValue = "10") Integer pageSize){
        PageInfo<Classification> list=classificationService.getClassificationList(pageNum,pageSize);
        return new APIResponse<>(ResponeCode.SUCCESS,"查询成功",list);
    }

    @Access(level = AccessLevel.ADMIN)
    @PostMapping("/create")
    @Transactional
    public APIResponse create(Classification classification){
        classificationService.creatClassification(classification);
        return new APIResponse<>(ResponeCode.SUCCESS,"创建成功");
    }

    @Access(level = AccessLevel.ADMIN)
    @PostMapping("/delete")
    public APIResponse delete(String ids){
        System.out.println("ids===" + ids);
        // 批量删除
        String[] arr = ids.split(",");
        for (String id : arr) {
            classificationService.deleteClassification(id);
        }
        return new APIResponse<>(ResponeCode.SUCCESS,"删除成功");
    }

    @Access(level = AccessLevel.ADMIN)
    @PostMapping("/update")
    @Transactional
    public APIResponse update(Classification classification){
        classificationService.updateClassification(classification);
        return new APIResponse<>(ResponeCode.SUCCESS,"更新成功");
    }
}
